% estima_fcastroll_evolve


nroll = length(firstdest:0.25:lastdest); 

parevolve_mat = zeros(numpar, nroll); 
logprior_roll = -1e15*ones(nroll, 1); 
loglikel_roll = -1e15*ones(nroll, 1); 
logpostd_roll = -1e15*ones(nroll, 1); 
optimat_roll  = zeros(3, nroll); 

parevolve_mat(:, 1)   = parmode_init; 
optimat_roll (:, 1)   = optimat_descend(:, 1);  



partrmat_est          =partrmat(parposest, :);
parvecmode            =zeros(numpar, 1);
parvecmode(parposcal) =parcalval;
errcount              =0;

cd(outpath); 
eval(['save ', savefname, ' -append']); 
cd(cucd); 

for ii = 1:nroll;
    
   dest = firstdest + 0.25*(ii - 1);
   dest_str = sample2date(dest, '');
   dest_str = dest_str{:}; 
   disp(['rolling end date: ', dest_str ]); 
    
    nper_est = find(dest == sample);
    Y = Yfull(1:nper_est, :);
    
    if 17 >= nper_est;
        error('not enough periods');
    end
    trainvec = [17, nper_est];
    if ii >= 2
        % use estimates from previous round as starting value
        xzero =  parevolve_mat(parposest, ii - 1);
        flag_optimok=1;
        tic;
        estima_loop;
        time=toc/60;
        
        if flag_optimok==0
            continue
        end
        
        parvecmode(parposest) = xestmode;
        parestmode = xestmode;
        parevolve_mat(:, ii) = parvecmode;
        
        optimat_roll(1, ii) = estimiter;
        optimat_roll(2, ii) = exitflag;
        optimat_roll(3, ii) = time;
        
    end
    
    [lpostd, likel] = lmj_postmax(xestmode,parvecmode,parposest,funcmod,Y,trainvec, prpar, prss, solveopt, addsol, ssposest);
    
    logpostd_roll(ii) = lpostd;
    loglikel_roll(ii) = likel;
    logprior_roll(ii) = lpostd - likel;
    
    % outforc=forc_dsge(Y,trainvec,funcmod,parvecmode,solveopt,addsol,nforc);  
    cd(outpath); 
    eval(['save ', savefname, ' -append']); 
    cd(cucd); 
end

cd(outpath); 
eval(['save ', savefname, ' -append']); 
cd(cucd); 